const { get,findByIn } = require("../../utils/db");
// const db = wx.cloud.database()

const app = getApp()
Page({

  data: {
    menuList: [],    //菜谱列表
    follows:[],       //关注列表
    isLogin: false, //是否登录。 false 未登录  true，已经登录
    userInfo: null,    //用户信息
    activeIndex: 0,
    recipes: [
      {
        recipeName: "烤苏格兰蛋",
        src: "../../imgs/1.jpg"
      },
      {
        recipeName: "法国甜点",
        src: "../../imgs/2.jpg"
      },
      {
        recipeName: "法式蓝带芝心猪排",
        src: "../../imgs/3.jpg"
      },
      {
        recipeName: "菠萝煎牛肉扒",
        src: "../../imgs/4.jpg"
      },
      {
        recipeName: "快手营养三明治",
        src: "../../imgs/5.jpg"
      },
      {
        recipeName: "顶级菲力牛排",
        src: "../../imgs/6.jpg"
      }
    ],
    types: [
      { typename: "营养菜谱", 'src': "../../static/type/type01.jpg" },
      { typename: "儿童菜谱", 'src': "../../static/type/type02.jpg" },
      { typename: "家常菜谱", 'src': "../../static/type/type03.jpg" },
      { typename: "主食菜谱", 'src': "../../static/type/type04.jpg" },
      { typename: "西餐菜谱", 'src': "../../static/type/type05.jpg" },
      { typename: "早餐菜谱", 'src': "../../static/type/type06.jpg" },
    ],
    lists: [
      {
        src: "../../static/list/list01.jpg",
        name: "土豆小番茄披萨",
        userInfo: {
          nickName: "林总小图",
          pic: "../../static/list/users.png"
        },
        views: 999,
        follow: 100
      },
      {
        src: "../../static/list/list02.jpg",
        name: "草莓巧克力三明治",
        userInfo: {
          nickName: "林总小图",
          pic: "../../static/list/users.png"
        },
        views: 88,
        follow: 200
      },
      {
        src: "../../static/list/list03.jpg",
        name: "法师意大利面",
        userInfo: {
          nickName: "林总小图",
          pic: "../../static/list/users.png"
        },
        views: 999,
        follow: 100
      },
      {
        src: "../../static/list/list04.jpg",
        name: "自制拉花",
        userInfo: {
          nickName: "林总小图",
          pic: "../../static/list/users.png"
        },
        views: 999,
        follow: 100
      },
      {
        src: "../../static/list/list05.jpg",
        name: "营养早餐",
        userInfo: {
          nickName: "林总小图",
          pic: "../../static/list/users.png"
        },
        views: 999,
        follow: 100
      }
    ],
  },
  _delStyle() {
    wx.showModal({
      title: "删除提示",
      content: "确定要删除么？",

    })
  },
  toCategory(){
    wx.navigateTo({
      url: '/pages/category/category',
    })
  },
  async onLoad() {
    //获取我发布的菜谱
    this.getMyMenu()

    //获取用户头像和昵称
    this.getUser()

    //获取我关注的菜谱
    this.getFollows()



  },
  //获取我关注的菜谱列表
  async getFollows() {
    //从follows集合中，获取我关注的menuid（）
    let openid = wx.getStorageSync('openid')

    //根据openid查询follows，获取menuid
    let result = await get({ collection: "follows", where: { _openid: openid } }).catch(err => {
      console.error(err)
    })
    //整理数组，把对象数组转成字符串数组
    let arr = result.data.map(item => {
      return item.menuid
    })
  
    // let res = await db.collection("menu").where({
    //   _id: db.command.in(arr)
    // }).get().catch(err => console.error(err))
    // console.log(res)
    //根据字符串数组，查询menu集合
    let res=await findByIn({collection:"menu",arr:arr}).catch(err=>{
      console.error(err)
    })
    //渲染到页面
    this.setData({
      follows:res.data
    })

  },
  //我发布的菜谱
  async getMyMenu() {
    //获取openid
    let openid = wx.getStorageSync('openid')

    //根据openid查询menu集合
    let result = await get({ collection: "menu", where: { _openid: openid } })
      .catch(err => console.error(err))

    //把结果渲染到页面
    this.setData({
      menuList: result.data
    })
  },
  getUser() {
    //获取用户头像
    let userInfo = app.globalData.userInfo;
    if (userInfo == null) {
      app.myInfo = (res) => {
        this.setData({
          userInfo: res.userInfo,
          isLogin: true
        })
      }
    } else {
      this.setData({
        userInfo,
        isLogin: true
      })
    }
  },
  //获取用户公开信息
  getInfo(e) {
    this.setData({
      userInfo: e.detail.userInfo,
      isLogin: true
    })
  },
  //点击tab栏目
  tabClick(e) {
    // console.log(e)
    let activeIndex = e.currentTarget.id;
    this.setData({
      activeIndex
    })
  },
  //跳转添加页面
  toAdd() {
    wx.navigateTo({
      url: '/pages/pbrecipe/pbrecipe',
    })
  }

})